// src/main/java/com/example/musicplayer/FindFragment.kt
package com.example.musicplayer

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.musicplayer.dao.MusicDAOImpl
import com.example.musicplayer.model.Song
import com.example.musicplayer.util.CategoryDatabaseHelper

class FindFragment : Fragment() {

    private lateinit var recyclerViewSongs: RecyclerView
    private lateinit var add_button: ImageButton
    private lateinit var musicDAO: MusicDAOImpl
    private lateinit var categoryDatabaseHelper: CategoryDatabaseHelper
    private lateinit var songAdapter: SongAdapter

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_find, container, false)

        recyclerViewSongs = view.findViewById(R.id.recyclerViewSongs)
        add_button = view.findViewById(R.id.add_botton)
        musicDAO = MusicDAOImpl(requireContext())
        categoryDatabaseHelper = CategoryDatabaseHelper(requireContext())

        setupRecyclerView()

        add_button.setOnClickListener {
            val intent = Intent(requireContext(), AddSongActivity::class.java)
            startActivity(intent)
        }

        return view
    }

    private fun setupRecyclerView() {
        songAdapter = SongAdapter(emptyList(), object : SongAdapter.OnItemClickListener {
            override fun onItemClick(song: Song) {
                navigateToMusicFragment(song)
            }
        })
        recyclerViewSongs.layoutManager = LinearLayoutManager(requireContext())
        recyclerViewSongs.adapter = songAdapter
    }

    private fun navigateToMusicFragment(song: Song) {
        val fragmentManager: FragmentManager = requireActivity().supportFragmentManager
        val fragmentTransaction = fragmentManager.beginTransaction()

        val musicFragment = MusicFragment()
        val bundle = Bundle()
        bundle.putSerializable("song", song) // 确保 Song 类实现了 Serializable 接口
        musicFragment.arguments = bundle

        fragmentTransaction.replace(R.id.fragment_container, musicFragment)
        fragmentTransaction.addToBackStack(null)
        fragmentTransaction.commit()
    }

    override fun onResume() {
        super.onResume()
        loadSongs()
    }

    private fun loadSongs() {
        val allSongs = musicDAO.getAllSongs() // 确保这个方法返回所有歌曲
        if (allSongs.isEmpty()) {
            Log.w("FindFragment", "No songs found in the database")
        } else {
            Log.d("FindFragment", "Loaded ${allSongs.size} songs from the database")
        }
        songAdapter.updateData(allSongs)
    }
}